<?php

/**
 * StoreItem filter form base class.
 *
 * @package    ProdAssistant
 * @subpackage filter
 * @author     Paweł Świtalla
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 29570 2010-05-21 14:49:47Z Kris.Wallsmith $
 */
abstract class BaseStoreItemFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'name'        => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'quantity'    => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'category_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Category'), 'add_empty' => true)),
      'demand_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'ProductionDemand')),
      'wz_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'WZ')),
      'order_list'  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'StoreOrder')),
    ));

    $this->setValidators(array(
      'name'        => new sfValidatorPass(array('required' => false)),
      'quantity'    => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'category_id' => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Category'), 'column' => 'id')),
      'demand_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'ProductionDemand', 'required' => false)),
      'wz_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'WZ', 'required' => false)),
      'order_list'  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'StoreOrder', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('store_item_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addDemandListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.DemandItems DemandItems')
      ->andWhereIn('DemandItems.demand_id', $values)
    ;
  }

  public function addWZListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.WZItems WZItems')
      ->andWhereIn('WZItems.wz_id', $values)
    ;
  }

  public function addOrderListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.OrderItems OrderItems')
      ->andWhereIn('OrderItems.order_id', $values)
    ;
  }

  public function getModelName()
  {
    return 'StoreItem';
  }

  public function getFields()
  {
    return array(
      'id'          => 'Number',
      'name'        => 'Text',
      'quantity'    => 'Number',
      'category_id' => 'ForeignKey',
      'demand_list' => 'ManyKey',
      'wz_list'     => 'ManyKey',
      'order_list'  => 'ManyKey',
    );
  }
}
